#include "dswlanmsgsession.h"

CDswLanMsgSession::CDswLanMsgSession()
{
}

CDswLanMsgSession::~CDswLanMsgSession()
{
}

CDswLanMsgSession* CDswLanMsgSession::createNew(LPST_ENV_T pEnv, struct event_base *base, int nfd, char* pKey)
{
	struct timeval tv = {15, 0};
	struct bufferevent *pBev = bufferevent_socket_new(base, nfd, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE);
	bufferevent_set_timeouts(pBev, &tv, NULL);
	return new CDswLanMsgSession(pEnv, pBev, sizeof(int), pKey);
}

CDswLanMsgSession::CDswLanMsgSession(LPST_ENV_T pEnv, struct bufferevent * pBev, int nHeadSize, char* pKey)
:  CDswmessagesession(pEnv, pBev, nHeadSize)
{
	CMarkup xml;
	
	strcpy(m_sKey, pKey);
	MakeReportPUA(xml);
	SendMsg((char*)xml.GetDoc().c_str());
}

void CDswLanMsgSession::MakeReportPUA(CMarkup & xml)
{
	char sip[64], sbuf[32], sid[32];
	int ch_num, nNum;
	void* pParam;
	
	xml.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
	xml.AddElem("Message");
	xml.AddAttrib("Version", "1.0.0");
	xml.AddChildElem("DSW_HEADER");
	xml.IntoElem();
		xml.AddChildElem("MessageType", "MSG_PUA_REGIST");
	xml.OutOfElem();
	xml.AddChildElem("DSW_KEY", defDSWKEY);
	m_pEnv->pOnNotifyFunc(PMSG_GET_BASIC, pParam);
	xml.AddChildElem("DSW_PUA_ID", ((LPST_DEVICE_BASE)pParam)->devid);
	pParam = (void*)sid;
	m_pEnv->pOnNotifyFunc(PMSG_GET_DEVTYPE, pParam);
	xml.AddChildElem("PUA_TYPE", *((int*)pParam));
	xml.AddChildElem("DSW_PUA_SN", "");
	pParam = (void*)sip;
	m_pEnv->pOnNotifyFunc(PMSG_GET_ACTIVEIP, pParam);
	if (strlen(sip) > 0){
		xml.AddChildElem("DSW_PUA_IP", sip);
		xml.AddChildElem("DSW_PUA_PORT", 80);
		m_pEnv->pOnNotifyFunc(PMSG_GET_NETINFO, pParam);
		xml.AddChildElem("DSW_PUA_MAC", ((LPST_LAN_PARAM)pParam)->mac);
		m_pEnv->pOnNotifyFunc(PMSG_GET_LOCATE, pParam);
		xml.AddChildElem("DSW_PUA_LOCATE", ((LPST_LOCATION_PARAM)pParam)->dev_location);
		m_pEnv->pOnNotifyFunc(PMSG_GET_ALARMINFO, pParam);
		xml.AddChildElem("DSW_PUA_LOCK", ((LPST_ALARM_PARAM)pParam)->enalarm);
		m_pEnv->pOnNotifyFunc(PMSG_GET_OSVER, pParam);
		xml.AddChildElem("DSW_OSVER", (char*)pParam);
		m_pEnv->pOnNotifyFunc(PMSG_GET_APPVER, pParam);
		xml.AddChildElem("DSW_APPVER", (char*)pParam);
		pParam = (void*)sip;
		m_pEnv->pOnNotifyFunc(PMSG_GET_VIDEOLISTEN_PORT, pParam);
		xml.AddChildElem("DSW_VPORT", *((int*)pParam));
		xml.AddChildElem("DSW_CHN", m_pEnv->nMaxChannel);
		for (ch_num = 0; ch_num < m_pEnv->nMaxChannel; ch_num++){
			sprintf(sbuf, "CH%d", ch_num);
			xml.AddChildElem(sbuf);
			xml.IntoElem();
				xml.AddChildElem("DSW_STREAM", "1;0");
				nNum = ch_num;
				pParam = (void*)&nNum;
				m_pEnv->pOnNotifyFunc(PMSG_GET_VIDEOINFO, pParam);
				xml.AddChildElem("LOCATE", ((LPST_CH_PARAM)pParam)->ch_location);
			xml.OutOfElem();
		}
		if (m_pEnv->nMaxChannel == 4){
			m_pEnv->pOnNotifyFunc(PMSG_GET_EXTERNAL_CAMS, pParam);
			nNum = 0;
			for (ch_num = 0; ch_num < MAX_EXTERNAL_CAMS; ch_num++){
				if ((strlen(((LPST_EXTERNAL_CAMS_T)pParam)->nExtCam[ch_num].sUUID) > 0) &&
					(strcmp(((LPST_EXTERNAL_CAMS_T)pParam)->nExtCam[ch_num].sType, "ONVIF") == 0))
					nNum++;
			}
			xml.AddChildElem("DSW_EXCHN", nNum);
			nNum = 0;
			for (ch_num = 0; ch_num < MAX_EXTERNAL_CAMS; ch_num++){
				if ((strlen(((LPST_EXTERNAL_CAMS_T)pParam)->nExtCam[ch_num].sUUID) > 0) && 
					(strcmp(((LPST_EXTERNAL_CAMS_T)pParam)->nExtCam[ch_num].sType, "ONVIF") == 0)){
					sprintf(sbuf, "EXCH%d", nNum);
					xml.AddChildElem(sbuf);
					xml.IntoElem();
						xml.AddChildElem("UUID", ((LPST_EXTERNAL_CAMS_T)pParam)->nExtCam[ch_num].sUUID);
						ST_EXTERNAL_CAM_PROPERTY_T nExtPropty[2] = {0};
						ST_EXTERNAL_CAM_DETAIL_T nExtDetail;
						void* pParamExt;
						strcpy(nExtDetail.nExtCam.sUUID, ((LPST_EXTERNAL_CAMS_T)pParam)->nExtCam[ch_num].sUUID);
						nExtDetail.nTotalStream = 0;
						nExtDetail.pStreamProperty = nExtPropty;
						pParamExt = (void*)&nExtDetail;
						m_pEnv->pOnNotifyFunc(PMSG_GET_EXTERNAL_CAM_STATUS, pParamExt);
						xml.AddChildElem("ONLINE", nExtDetail.nExtCam.bOnLine);
						if (nExtDetail.nTotalStream >= 2)
							xml.AddChildElem("DSW_STREAM", "1;0");
						else
							xml.AddChildElem("DSW_STREAM", "1");
						xml.AddChildElem("MAIN_STREAM", nExtPropty[0].sStreamURL);
						xml.AddChildElem("SUB_STREAM", nExtPropty[1].sStreamURL);
					xml.OutOfElem();
					nNum++;
				}
			}
		}
		xml.AddChildElem("DSW_RESULT");
		xml.IntoElem();
			xml.AddChildElem("Errcode", 0);
		xml.OutOfElem();
	}
}

void CDswLanMsgSession::MakeRespXML(const char* pMsgType, CMarkup & xml, int nerrcode)
{
	void* pParam;
	xml.SetDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	xml.AddElem("Message");
	xml.AddAttrib("Version", "1.0.0");
	xml.AddChildElem("DSW_HEADER");
	xml.IntoElem();
		xml.AddChildElem("MessageType", pMsgType);
	xml.OutOfElem();
	xml.AddChildElem("DSW_KEY", m_sKey);
	m_pEnv->pOnNotifyFunc(PMSG_GET_BASIC, pParam);
	xml.AddChildElem("DSW_PUA_ID", ((LPST_DEVICE_BASE)pParam)->devid);
	xml.AddChildElem("DSW_PUA_SN", "");
	xml.AddChildElem("DSW_RESULT");
	xml.IntoElem();
	xml.AddChildElem("Errcode", nerrcode);
	xml.OutOfElem();
}

void CDswLanMsgSession::SendMsg(char * pMsg)
{
//	char sBuffer[1024 * 5];
//	*((int*)sBuffer) = strlen(pMsg);
//	memcpy(sBuffer + sizeof(int), pMsg, strlen(pMsg));
//	int nMsgLen = strlen(pMsg) + sizeof(int);
//	evbuffer_add(bufferevent_get_output(m_pBev), sBuffer, nMsgLen);
	int nLen = strlen(pMsg);
	evbuffer_add(bufferevent_get_output(m_pBev), &nLen, sizeof(int));
	evbuffer_add(bufferevent_get_output(m_pBev), pMsg, nLen);
}

